<template>
  <div class="blog-detail">
    <h2>{{blog.title}}</h2>
    <div class="information">{{blog.type}}&nbsp;&nbsp;&nbsp;&nbsp;{{blog.timestamp}}</div>
    <div class="description">摘要：{{blog.description}}</div>
    <el-image :src="blog.url" v-if="blog.type == '锚点'"></el-image>
    <div class="line"></div>
    <div class="content markdown-body" v-html="blog.content"></div>
  </div>
</template>

<script>
import 'github-markdown-css/github-markdown.css'//md美化样式
import router from '@/router'
import { ref} from '@vue/reactivity'
import axios from 'axios'
import { onBeforeMount } from '@vue/runtime-core'
import { onBeforeRouteUpdate } from 'vue-router'

export default {
    name: 'BlogDetail',
    setup() {
        let blog = ref({})

        //id参数配置默认值提高代码利用率
        const getBlog = (id = router.currentRoute.value.params.id) => {
            axios.get('http://localhost/api/queryblog?id='+id).then(res => {
                blog.value = res.data//用reactive会出问题
                // console.log(blog.value)
            }).catch(err => {
                console.log('未知错误！')
                console.log(err)
            })
        }

        onBeforeMount(() => {
            getBlog()
        })
        //连续复用组件也要更新数据
        onBeforeRouteUpdate((to) => {
            getBlog(to.params.id)
        })

        return {
            blog
        }
    }
}
</script>

<style scoped>
    .blog-detail {
        width: 600px;
        margin: 0 50px;
        padding: 20px 50px;
        box-shadow: var(--el-box-shadow-light);
    }
    h2 {
        text-align: center;
    }
    .information {
        text-align: center;
        font-size: 14px;
    }
    .line {
        height: 1px;
        margin: 10px 0;
        background-color: #E4E7ED;
    }
    .description {
        margin: 20px 0;
    }
    
</style>